//
//  PowerMeterPage.swift
//  LtwooEletrics
//
//  Created by K YTYan on 2024/10/10.
//

import SwiftUI


 
@available(iOS 13.0, *)
struct PowerMeterListsModel: Equatable, Identifiable {
    static func == (lhs: PowerMeterListsModel, rhs: PowerMeterListsModel) -> Bool {
        return lhs.id == rhs.id
    }
    var id = UUID()
    var title: String
    var content: String = ""
    var showArrow: Bool = false
    var isEvent: Bool = true
    
    //var operateBlock: ((String)->Void)? = nil
    
}

@available(iOS 13.0, *)
class PowerMeterViewModel: ObservableObject {
    @Published var items: [PowerMeterListsModel] = [
        PowerMeterListsModel(title: "功率",content: "0 w", isEvent: false)
        ,PowerMeterListsModel(title: "踏频",content: "0 rpm", isEvent: false)
       ,PowerMeterListsModel(title: "左右平衡" )
       ,PowerMeterListsModel(title: "踩踏平顺度")
       ,PowerMeterListsModel(title: "倒链校准")
        ,PowerMeterListsModel(title: "功率灵敏度",content: "100%", showArrow: true)
    ]
    @Published var isSmoothness: Bool = false
    @Published var isBalance: Bool = false
}


@available(iOS 14.0, *)
struct PowerMeterPageTest: View {
    
  
    @State var toggleCange:((String, Bool)->Void)? = nil
    @State var operateBlock: ((String)->Void)? = nil
    @StateObject private var viewModel = PowerMeterViewModel()
 
 
    @available(iOS 14.0, *)
    var body: some View {
        
        ZStack {
            VStack {
                
                VStack {
                    ForEach($viewModel.items) {  $item  in
                        if item.isEvent {
                            Button(action: {
                                let _title = item.title //items[_index].title
                                print("点击了\(_title)")
                                if _title == "左右平衡" {
                                    viewModel.isBalance = !viewModel.isBalance
                                    toggleCange?(_title, viewModel.isBalance)
                                }else if   _title == "踩踏平顺度" {
                                    viewModel.isSmoothness = !viewModel.isSmoothness
                                    toggleCange?(_title, viewModel.isSmoothness)
                                }else {
                                    operateBlock?(_title)
                                }
                                
                            }, label: {
                                setCell(item: item)
                            })
                        }else {
                            setCell(item: item)
                        }
                    }
                }
                .frame(height:  UIScreen.main.bounds.height * 0.5)
//                .background(Color.blue)
                .cornerRadius(12.0)
                .padding(.vertical,10)
            
                
                
                
                Button {
                    operateBlock?("零点校准")
                } label: {
                    Text("零点校准").frame(width: UIScreen.main.bounds.width * 0.8, height: 44).foregroundColor(Color.white).background(Color.blue)
                }
                .frame(maxHeight: .infinity,alignment: .bottom)
                
                Spacer()
            }
        }
        .background(Color.red.edgesIgnoringSafeArea(.all).frame(width: UIScreen.main.bounds.width))
        .onAppear {
            Dlog("???")
            //self.viewModel.items[2].state = true
            self.viewModel.isBalance = true
        }
    }
    
    func setCell(item:PowerMeterListsModel) -> some View {
        
        return  HStack {
            let _item = item
            if _item.title == "左右平衡" || _item.title == "踩踏平顺度" {
                
                let _isOn : Binding<Bool> = _item.title == "左右平衡" ? $viewModel.isBalance : $viewModel.isSmoothness
                Toggle(_item.title, isOn: _isOn)

            }else {
                Text(_item.title)
                Spacer()

                
                HStack(spacing: 0) {
                    Text(_item.content)
                    if _item.showArrow == true {
                        Image("ringthArrow").resizable().frame(width: SUITABLE(30), height: SUITABLE(30)).colorMultiply(Color(UIColor(hexString: "#747474")!))
                    }
                }.offset(x: _item.showArrow ? 10 : -2).foregroundColor(Color(UIColor(hexString: "#747474")!))

            }
                
        }.frame(height: 44).padding(5).background(Color(UIColor(hexString: "#171717")!))
         .foregroundColor(.white)
        
    }
}

@available(iOS 14.0, *)
#Preview {
    PowerMeterPageTest()
}
